package exercises;

import jakarta.servlet.ServletException;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-07-12
 * Time:18:37
 */
@WebServlet(urlPatterns = "/pig",loadOnStartup = 1)
public class PigServlet extends HttpServlet {
    private static int getCount = 0;
    private static int postCount = 0;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("PigServlet - doGet time: " + (++getCount));
        if (req.getRemoteAddr().equals("0:0:0:0:0:0:0:1")) {
            System.out.println("Requested IP address: 127.0.0.1");
        } else {
            System.out.println("Requested IP address: " + req.getRemoteAddr());
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("PigServlet - doPost time: " + (++postCount));
        if (req.getRemoteAddr().equals("0:0:0:0:0:0:0:1")) {
            System.out.println("Requested IP address: 127.0.0.1");
        } else {
            System.out.println("Requested IP address: " + req.getRemoteAddr());
        }
    }
}
